﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Exam.BLL;
using Exam.Model.ExtentionModels;
using Exam.Model;
using ExaminationWeb.Common;
using Exam.DBHelper;
using System.Data.SqlClient;
namespace ExaminationWeb.Controllers
{
    public class QuestionsController : Controller
    {
        //
        // GET: /Questions/
        ModalityService modalityService = new ModalityService();
        QuestionsService questionService = new QuestionsService();
        public ActionResult QuestionList()
        {
            return View();
        }
        public ActionResult QuestionAdd()
        {
            ViewData["ModalityList"] = modalityService.LoadEntites();
            return View();
        }
        public ActionResult QuestionUpdate()
        {
            return View();
        }

        [HttpPost]
        public string AddInfo(ExQuestions exquestion)
        {
          
            try
            {
                Questions questions = new Questions();
                questions.QuestionID = Guid.NewGuid();
                questions.Block = exquestion.Block;
                DateTime time = DateTime.Now;
                questions.CreateTime = time;
                questions.CreateUserID = UserAccount.UserID;
                questions.IsActive = exquestion.IsActive;
                questions.LibraryID = exquestion.LibraryID;
                questions.ModalityID = exquestion.ModalityID;
                questions.ProductID = exquestion.ProductID;
                questions.QLevel = exquestion.QLevel;
                questions.QuestionContent = exquestion.QuestionContent;
                questions.QuestionType = exquestion.QuestionType;
                questions.UpdateTime = time;
                questions.Answer = exquestion.Answer;
                if (IsExistData(questions).QuestionID.ToString() == "00000000-0000-0000-0000-000000000000")
                {


                  //  List<Answer> answerlist = exquestion.AnswerList;
                    //Questions newQuestion = questionService.AddEntity(questions);
                    //return newQuestion.QuestionID.ToString();
                    return "";
                }
                else
                {
                    return "repeat";
                }

            }
            catch (Exception e)
            {
                return "";
            }
        }


        #region 公用
        public Questions IsExistData(Questions question)
        {
            SQLHelper commonservice = new SQLHelper();
            Dictionary<string, SqlParameter> dic = new Dictionary<string, SqlParameter>();

            SqlParameter partype = new SqlParameter("@QuestionType", System.Data.SqlDbType.Int);
            partype.Value = question.QuestionType;
            dic.Add("QuestionType", partype);

            SqlParameter parproduct = new SqlParameter("@ProductID", System.Data.SqlDbType.UniqueIdentifier);
            parproduct.Value = question.ProductID;
            dic.Add("ProductID", parproduct);

            SqlParameter parlevel = new SqlParameter("@QLevel", System.Data.SqlDbType.Int);
            parlevel.Value = question.QLevel;
            dic.Add("QLevel", parlevel);

            SqlParameter parblock = new SqlParameter("@Block", System.Data.SqlDbType.Int);
            parblock.Value = question.Block;
            dic.Add("Block", parblock);

            SqlParameter parcontent = new SqlParameter("@QuestionContent", System.Data.SqlDbType.Int);
            parcontent.Value = question.QuestionContent;
            dic.Add("QuestionContent", parcontent);

            return commonservice.getExistData<Questions>("Questions", dic);
        }

        #endregion
    }
}
